<?php


namespace app\impl;


use app\common\lib\Redis;
use app\common\lib\ZhyxMongo;
use app\common\model\Follow;
use app\common\model\TaskDetail;
use app\interfaces\Task;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use think\facade\Log;

class EmployeeGranule implements Task
{

    public function run(array $data,string $taskId): ?bool
    {
        try {

            $startDate = $data["startDate"] ?? date("Y-m-d", strtotime("-1 day"));
            //判断当日数据是否已经被计算
            $isRun = Redis::getInstance()->redis->hget("data:suifang", $startDate);
            $isRun = [];
            if (empty($isRun)) {

                //执行任务统计
                $result = ZhyxMongo::getInstance()->setCollection('follow')->aggregate([['$unwind' => ['path' => '$task', 'preserveNullAndEmptyArrays' => true]], ['$lookup' => ['from' => 'follow_history', 'localField' => '_id', 'foreignField' => 'follow_id', 'as' => 'follow_result']], ['$unwind' => ['path' => '$follow_result', 'preserveNullAndEmptyArrays' => true]], ['$match' => ['$or' => [['follow_result.finish_date' => $startDate], ['follow_result.pushtime' => $startDate], ['create_date' => $startDate]], "deleted_at" => 0]], ['$group' => ['_id' => ['staff_id' => '$follow_staff.user_id', 'task_id' => '$task_id'], 'dept_id' => ['$first' => '$follow_staff.department_id'], 'department_name' => ['$first' => '$follow_staff.department_name'], 'staff_id' => ['$first' => '$follow_staff.user_id'], 'staff_user_name' => ['$first' => '$follow_staff.username'], 'task_id' => ['$first' => '$task_id'], 'task_name' => ['$first' => '$taskname'], 'sms_total' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.mode', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_total' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.mode', 4]]]], 'then' => 1, 'else' => 0]]], 'duration' => ['$sum' => '$follow_result.duration'], 'task_result_detail' => ['$addToSet' => '$pid'], 'sms_completed' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.mode', 2]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_completed' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.mode', 4]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_mzsc_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 25]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_zysc_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 24]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_wd_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 23]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_zy_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 22]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_sw_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 19]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_jsjt_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 18]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_wrjt_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 17]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_hmcw_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 16]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_wfjt_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 15]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_tj_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 14]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_gj_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 13]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_zdjj_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 12]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_qtqk_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 11]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_kh_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 10]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_hmwx_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 9]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'phone_jjsf_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 8]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]], 'task_completed_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$followStatus', 1]]]], 'then' => 1, 'else' => 0]]], 'phone_ff_number' => ['$sum' => ['$cond' => ['if' => ['$and' => [['$eq' => ['$follow_result.result', 6]], ['$eq' => ['$follow_result.status', 2]]]], 'then' => 1, 'else' => 0]]]]]]);

                if ($result !== null) {
                    $arr = [];
                    foreach ($result as $k => $v) {
                        $v["task_num"] = count($v["task_result_detail"]);
                        unset($v["task_result_detail"]);
                        $taskFinishNumber = ZhyxMongo::getInstance()->setCollection('follow')->aggregate([['$lookup' => ['from' => 'follow_history', 'localField' => '_id', 'foreignField' => 'follow_id', 'as' => 'follow_result']], ['$match' => ['task_id' => $v["_id"]["task_id"], 'follow_staff.user_id' => $v["_id"]["staff_id"], 'followStatus' => 1, 'follow_result.finish_date' => $startDate]], ['$count' => 'task_completed']]);
                        foreach ($taskFinishNumber as $kk => $vv) {
                            $v["task_completed_number"] = $vv["task_completed"];
                        }
                        $v["start_date"] = new UTCDateTime(strtotime($startDate) * 1000 + 28800000);
                        unset($v["_id"]);
                        $arr[] = (array)$v;
                    }

                    if (!empty($arr)) {

                        $result = ZhyxMongo::getInstance()->setCollection('follow_report')->insertMany($arr);

                    } else {
                        Log::record($startDate . "没有新增随访任务数据", "follow");
                    }
                }
                Redis::getInstance()->redis->hset("data:suifang", $startDate, 1);
            }
            return true;
        } catch (\Exception $exception) {

            Log::record("同步随访数据：" . $startDate . "失败，失败原因:" . $exception->getMessage(), "follow");
            return false;
        }


    }
}